<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
             v-has-permi="['meeting:invite:list']"
    >
      <el-form-item label="用户名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="部门名称" prop="dept">
        <el-select v-model="queryParams.dept">
          <el-option
            v-for="item in allDept"
            :key="item.deptId"
            :label="item.deptName"
            :value="item.deptId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select v-model="queryParams.state">
          <el-option label="未邀请" value="0"/>
          <el-option label="已邀请" value="1"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"
                   v-has-permi="['meeting:invite:Query']">搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery" v-has-permi="['meeting:invite:Query']">重置
        </el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-s-promotion"
          size="mini"
          @click="invited"
          :disabled="single"
          v-has-permi="['meeting:invite:add']"
        >发起预约
        </el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          @click="deleteMeeting"
          :disabled="single"
          v-has-permi="['meeting:invite:delete']"
        >删除预约
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="用户名称" align="center" prop="nickName"/>
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          <span>{{ scope.row.sex === 0 ? "男" : scope.row.sex === 1 ? "女" : "未知" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" prop="phonenumber"/>
      <el-table-column label="状态" align="center" prop="userId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.invite_terms_start" :value="scope.row.userId !== null ? '1' : '0' "/>
        </template>
      </el-table-column>
      <el-table-column label="部门" align="center" prop="deptName"/>
      <el-table-column label="岗位" align="center" prop="postName"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-promotion"
            @click="send(scope.row)"
            v-hasPermi="['meeting:invite:add']"
          >发起预约
          </el-button>
          <el-button
            v-hasPermi="['meeting:invite:delete']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            v-show="scope.row.userId !== null"
            @click="deleteMeeting(scope.row)"
          >删除预约
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="loadData"
    />

    <el-dialog width="30%" title="会议邀请" :visible.sync="dialogVisible">
      <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="100px">
        <el-form-item label="会议名称" prop="meetingName">
          <el-input v-model="formData.meetingName" placeholder="请输入会议名称" clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
        <el-form-item label="日期选择" prop="meetingTime">
          <el-date-picker type="datetime" v-model="formData.meetingTime" format="yyyy-MM-dd HH:mm:ss"
                          value-format="yyyy-MM-dd HH:mm:ss" :style="{width: '100%'}" placeholder="请选择日期选择"
                          clearable>
          </el-date-picker>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="formData.password" placeholder="请输入密码" clearable show-password
                    :style="{width: '100%'}"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="close()">取消</el-button>
        <el-button type="primary" @click="handleConfirm()">确定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  getLoginUserInvitationInfo,
  handleQueryInvitationInfo,
  deleteMenuById,
  addTermsInvitationInfo
} from "@/api/system/invitation/conference"
import {allListDept} from "@/api/system/dept.js"

export default {
  name: "Invitation",
  dicts: ['invite_terms_start'],
  data() {
    return {

      // 遮罩层加载
      loading: false,

      // 记录数
      total: 0,

      showSearch: true,

      // 所有的部门
      allDept: [],

      // 表格的数据
      postList: [],

      // 邀请会议按钮是否禁用
      single: true,

      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: '',
        dept: '',
        state: '',
      },

      formData: {
        userId: [],
        meetingName: null,
        meetingDate: null,
        meetingTime: null,
        password: null,
      },
      rules: {
        meetingName: [{
          required: true,
          message: '请输入会议名称',
          trigger: 'blur'
        }],
        meetingTime: [{
          required: true,
          message: '请选择开始时间',
          trigger: 'change'
        }],
        password: [{
          required: true,
          message: '请输入会议密码',
          trigger: 'blur'
        }],
      },
      // 对话框
      dialogVisible: false,

      // 多选表格数据
      ids: [],
      invitedIds: [],
      // 邮件
      email:[]
    }
  },
  created() {
    this.getAllDept()
    this.loadData()
  },
  methods: {
    /** 搜索按钮操作 */
    handleQuery() {
      this.loading = true
      this.queryParams.pageNum = 1
      handleQueryInvitationInfo(this.queryParams).then((response) => {
        this.postList = response.rows
        this.total = response.total
      })
      this.loading = false
      this.resetForm("queryForm")
    },
    // 重置按钮
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 页面加载时加载数据
    loadData() {
      this.loading = true
      getLoginUserInvitationInfo(this.queryParams).then(res => {
        this.postList = res.rows
        this.total = res.total
      })
      this.loading = false
    },
    // 查询部门信息
    getAllDept() {
      allListDept().then(res => {
        this.allDept = res.data
      })
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.single = selection.length === 0
      this.ids = []
      this.email = []
      selection.forEach(item => {
        this.ids.push(item.askedId)
        this.email.push(item.email)
        this.invitedIds.push(item.id)
      })
    },
    // 发送邀请d多选
    invited() {
      this.dialogVisible = true
      this.formData.userId = this.ids
    },
    // 发送邀请单选
    send(row) {
      this.dialogVisible = true
      this.email = []
      this.email.push(row.email)
      this.formData.userId = [row.askedId]
    },
    // 删除会议
    deleteMeeting(row) {
      this.$confirm('此操作将永久取消该邀请, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let id
        if (row.id) {
          id = row.id
        } else {
          id = this.invitedIds
        }
        deleteMenuById(id).then(res => {
          // 从新调用页面加载
          this.loadData()
          // 提示信息
          this.$message({
            type: 'success',
            message: `已取消${res.data}个会议!`
          });
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    // 关闭对话框
    close() {
      // 打卡对话框
      this.dialogVisible = false
      // 取消的提醒
      this.$message('您已取消邀请');
      // 重置表单
      this.formData = {
        meetingName: '',
        meetingDate: null,
        meetingTime: '',
        password: '',
      }
      // 重置表单校验
      this.$refs['elForm'].resetFields()
    },
    // 校验表单并发送邀请单选
    handleConfirm() {
      this.$refs['elForm'].validate(valid => {
          if (valid) {
            const formData = this.formData;
            formData.email = this.email;
            addTermsInvitationInfo(formData).then(res => {
              this.$message({
                type: 'success',
                message: `成功邀请${res.data}人!`
              });
              this.$refs['elForm'].resetFields()
              // 清空表单
              this.formData = {
                userId: [],
                meetingName: '',
                meetingDate: null,
                meetingTime: '',
                password: '',
              }
              // 关闭对话框
              this.dialogVisible = false
              // 在查询一遍数据
              this.loadData()
            })
          }
        }
      )
    },
  }
}
</script>
